package com.shujia.core

import com.shujia.core.Demo07GroupBy.StuGrp
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo13Union {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo13Union")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val stuLineRDD: RDD[String] = sc.textFile("spark/data/students.txt")

    // 将每条数据转换成样例类对象
    val stuRDD: RDD[StuGrp] = stuLineRDD.map(line => {
      val splits: Array[String] = line.split(",")
      StuGrp(splits(0), splits(1), splits(2).toInt, splits(3), splits(4))
    })


    /**
     * union : 将两份相同格式的数据进行纵向拼接，即SQL中的union(all)连接操作
     * 默认不会去重，所以相当于union all操作
     * 如果需要去重，则可以在union之后使用distinct进行去重
     */
    val sample01: RDD[StuGrp] = stuRDD.sample(withReplacement = false, fraction = 0.01, 1)
    val sample02: RDD[StuGrp] = stuRDD.sample(withReplacement = false, fraction = 0.01, 1)

    sample01.union(sample02).foreach(println) // union all
    sample01.union(sample02).distinct().foreach(println) // union


  }

}
